package com.manong.web.controller;

import com.github.pagehelper.PageInfo;
import com.manong.data.ResultData;
import com.manong.domain.Store;
import com.manong.query.StoreQueryObject;
import com.manong.service.StoreService;
import com.manong.util.JsonResult;
import com.manong.util.RequiredPermission;
import com.manong.vo.GoodsStoreVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/store")
@CrossOrigin(allowCredentials = "true")
public class StoreController {

    @Autowired
    private StoreService storeService;

    /**
     * 查询门面列表
     * @param queryObject
     * @return
     */
    @GetMapping("/list")
    @RequiredPermission(name = "查询门面列表", expression = "store:getList")
    public JsonResult list(StoreQueryObject queryObject) {
        PageInfo pageInfo =  storeService.query(queryObject);
        ResultData data = new ResultData(queryObject.getPageNum(),queryObject.getPageSize(),pageInfo.getList(),Integer.parseInt(pageInfo.getTotal()+""));
        return JsonResult.success(data);
    }

    /**
     * 删除门面接口
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @RequiredPermission(name = "门面删除", expression = "store:delete")
    public JsonResult delete(@PathVariable Integer id) {
        storeService.delete(id);
        return JsonResult.success();
    }


    /**
     * 新增或者修改门面
     * @param store
     * @return
     */
    @PostMapping("/saveOrUpdate")
    @RequiredPermission(name = "门面新增或者编辑", expression = "store:saveOrUpdate")
    public JsonResult saveOrUpdate(@RequestBody Store store) {
        storeService.saveOrUpdate(store);
        return JsonResult.success();
    }

    /**
     * 查询单个门面
     * @param id
     * @return
     */
    @GetMapping("/getStore/{id}")
    @RequiredPermission(name = "查询单个门面", expression = "store:getStore")
    public JsonResult getstore(@PathVariable Integer id) {
        Store store = storeService.get(id);
        // JsonResult data属性 role
        return JsonResult.success(store);
    }

    /**
     * 根据负责人查询门面
     * @param id
     * @return
     */
    @GetMapping("/getStoreByManager/{id}")
    @RequiredPermission(name = "根据负责人查询门面", expression = "store:getStoreByManager")
    public JsonResult getStoreByManager(@PathVariable Integer id) {
        Store store = storeService.getStoreByManager(id);
        // JsonResult data属性 role
        return JsonResult.success(store);
    }

    /**
     * 获取全部门面数量
     * @return
     */
    @GetMapping("/totalStoreCount")
    @RequiredPermission(name = "查询全部门面数量", expression = "store:totalStoreCount")
    public JsonResult totalStoreCount() {
        int totalStoreCount = storeService.getTotalStoreCount();
        return JsonResult.success(totalStoreCount);
    }

    /**
     * 查询门面中现存商品名称及其对应的数量
     * @param storeId
     * @return
     */
    @GetMapping("/getGoodsInStore/{storeId}")
    @RequiredPermission(name = "查询门面中现存商品名称及其对应的数量", expression = "store:getGoodsInStore")
    public JsonResult getGoodsInStore(@PathVariable Integer storeId) {
        List<GoodsStoreVo> goodsInStore = storeService.getGoodsInStore(storeId);
        return JsonResult.success(goodsInStore);
    }
}
